#include "drawer.h"
#include "chipplace.h"
#include "chip.h"
#include "woodensolitairescene.h"

Drawer::Drawer(int chipSize) : count(9), chipSize(chipSize), distance(chipSize+2)
{
}

void Drawer::draw(WoodenSolitaireScene *scene, const QColor &color)
{
    for(int i = 0; i < count; ++i){
        for(int j = 0; j < count; ++j){
            if(!(i < 3 && (j < 3 || j > 5) ||
               i > 5 && (j < 3 || j > 5)
              )) {

                int x = (i+1)*distance + i*chipSize;
                int y = (j+1)*distance + j*chipSize;
                ChipPlace *place = new ChipPlace(j, i, chipSize, color);
                place->setPos(x, y);
                scene->addPlace(place);

                if(i != 4 || j != 4) {
                    Chip *chip = new Chip(scene, j, i, chipSize, color);
                    chip->setPos(x, y);
                    scene->addChip(chip);
                }
            }
        }
    }
}

int Drawer::getPlaceIndex(int row, int col)
{
    if(row >= 3 && row <= 5){
        if(col >= 0 && col <= 2){
            row %= 3;
            return col*3 + row;
        }
        if(col >= 6 && col <= count){
            row %= 3;
            col %= 6;
            return 36 + col*3 + row;
        }
    }
    if(row >= 0 && row < count && col >= 3 && col <=5){
        col %= 3;
        return 9 + col*count + row;
    }
    return -1;
}

int Drawer::getChipSize()
{
    return chipSize;
}
